using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 预测逻辑模块
/// </summary>
public class ForecastManager : Singleton<ForecastManager>
{
    private CollisionManager collisionManager;
    private CorePrefab corePrefab;
    private PointManager pointManager;

    /// <summary>
    /// 用于缓存发射的子弹
    /// </summary>
    private Queue<BulletPrefabs> bulletPrefabs = new Queue<BulletPrefabs>();

    private void Start()
    {
        collisionManager = CollisionManager.Instance;
        if(CorePrefab.Instance == null)
            print("+++++++NULL");
        corePrefab = CorePrefab.Instance;
        pointManager = PointManager.Instance;
    }
    
    /// <summary>
    /// 是否有碰撞
    /// </summary>
    /// <param name="bullet">子弹</param>
    /// <returns></returns>
    public bool IsAttackCollisions(BulletPrefabs bullet)
    {
        bulletPrefabs.Enqueue(bullet);
        CollisionPrefab[] collsions = collisionManager.collisions;
        for (int i = 0; i < collsions.Length; i++)
        {
            if(IsAttackCollision(collsions[i],bullet))
            {
                return true;
            }
        }
        //击中core 得分
        //print("子弹的位置是：" + bullet.pos + "   碰撞层的位置是: " + corePrefab.pos + "   碰撞层的高度是" + corePrefab.height
        //    + "   子弹的速度是  " + bullet.speed);
        if (bullet == null)
        {
            print(("bullet is null"));
        }

        if (corePrefab == null)
        {
            print("core is null");
            print(CorePrefab.Instance);
        }
        float distance2 = (bullet.pos - corePrefab.pos - corePrefab.height);
        //print("得分！！   发射点到core的距离 " + distance2);
        float time2 = Mathf.Abs(distance2 / bullet.speed);
        //print("到core需要的时间：" + time2);
        Invoke("AttackCore", time2);

        return false;
    }

    /// <summary>
    /// 计算出单个层级的预测情况
    /// </summary>
    /// <param name="collisionPrefab">碰撞的层级实例</param>
    /// <param name="bullet">当前子弹</param>
    /// <returns></returns>
    private bool IsAttackCollision(CollisionPrefab collisionPrefab, BulletPrefabs bullet)
    {
        bool isAttack = false;
        //计算出到达该层需要的时间
        // print("子弹的位置是：" + bullet.pos + "   碰撞层的位置是: " + collisionPrefab.pos + "   碰撞层的高度是" + collisionPrefab.height
        //     + "   子弹的速度是  " + bullet.speed);
        print(bullet);
        print(collisionPrefab);
        float distance = (bullet.pos - collisionPrefab.pos - collisionPrefab.height);
        // print("发射点到该层的距离 " + distance);
        float time = Mathf.Abs( distance / bullet.speed);

        // print("需要花费的时间是 " + time);

        //计算出经过时间之后该层的位置
        float startPos, endPos;
        //print("collisionPrefab.currentAngle  " + collisionPrefab.currentAngle);
        float t = collisionPrefab.currentAngle - collisionPrefab.speed * time;
        startPos =  t < 0 ? t + 360 : t;
        t = startPos;
        startPos = t > 360 ? t - 360 : t;
        endPos = startPos - collisionPrefab.angle;
        //print("开始位置：  " + startPos + "  结束位置：" + endPos);

        //找出碰撞的情况
        if(endPos < 0)
        {
            isAttack = true;
            Invoke("AttackEffeck", time);
        } 
        print("是否碰撞  "+ isAttack + "  碰撞的层为" + collisionPrefab.gameObject.name);
        return isAttack;
    }

    private void AttackEffeck()
    {
        BulletPrefabs b = bulletPrefabs.Dequeue();
        print("扣分！！！！！！！！");
        pointManager.MinusScore();
        //if(b!=null && b.gameObject!=null)
            Destroy(b.gameObject);
        //Time.timeScale = 0;
        //TODO：
        //5. 命中阻挡层，阻挡层命中有反馈（弹开）
        
    }
    private void AttackCore()
    {
        BulletPrefabs b = bulletPrefabs.Dequeue();
        print("得分！！！！！！！！");
        pointManager.AddScore();
        //if (b != null && b.gameObject != null)
            Destroy(b.gameObject);
        //TODO：
        //0 回收

        //1.加分

        //2 相关的音效

    }
}
